package dmxGroupControl.solvers;

import dmxGroupControl.Fixture;
import javafx.scene.paint.Color;

import java.util.Arrays;
import java.util.List;

public class HSBSolver extends Solver
{
    static List<String> solvableChannels = Arrays.asList("Red", "Green", "Blue", "White", "MonoMaster" );
    static  List<String> solvableAttributes = Arrays.asList("Hue", "Saturation", "Brightness",
                                                                    "OverrideEnabled", "OverrideHue",
                                                                    "OverrideSaturation", "OverrideBrightness" );
    
    Fixture fixture;
	float hue, saturation, brightness;
    float overrideHue, overrideSaturation, overrideBrightness;
    float overrideEnabled;


    public HSBSolver(Fixture fixture)
	{
		this.fixture = fixture;
	}

	@Override
	protected void SolveInternal()
	{
		Color color;
        if(IsOverrideEnabled())
        {
            color = Color.hsb(overrideHue * 360.0f, overrideSaturation, overrideBrightness);
        }
        else
        {
            color = Color.hsb(hue * 360.0f, saturation, brightness);
        }

        if(IsFullColor())
        {
            fixture.GetChannels().OptionalSetChannel("Red", (float) (color.getRed()));
            fixture.GetChannels().OptionalSetChannel("Green", (float) (color.getGreen()));
            fixture.GetChannels().OptionalSetChannel("Blue", (float) (color.getBlue()));
        }
        else if(IsColor())
        {
            fixture.GetChannels().OptionalSetChannel("Red", (float) (color.getRed()) * saturation);
            fixture.GetChannels().OptionalSetChannel("Green", (float) (color.getGreen()) * saturation);
            fixture.GetChannels().OptionalSetChannel("Blue", (float) (color.getBlue()) * saturation);
        }
        fixture.GetChannels().OptionalSetChannel("White", brightness * (1 - saturation));
        fixture.GetChannels().OptionalSetChannel("MonoMaster", brightness);

	}
	
    boolean IsFullColor()
    {
        return fixture.GetChannels().Contains("Red") && fixture.GetChannels().Contains("Green") && fixture.GetChannels().Contains("Blue");
    }

    boolean IsColor()
	{
		return fixture.GetChannels().Contains("Red") || fixture.GetChannels().Contains("Green")
				|| fixture.GetChannels().Contains("Blue");
	}
	
	public void Set(String attributeName, float value)
	{
		switch(attributeName)
		{
		case "Hue":
			hue = value;
			break;
		case "Saturation":
			saturation = value;
			break;
		case "Brightness":
			brightness = value;
			break;
        case "OverrideEnabled":
            overrideEnabled = value;
            break;
        case "OverrideHue":
            overrideHue = value;
            break;
        case "OverrideSaturation":
            overrideSaturation = value;
            break;
        case "OverrideBrightness":
            overrideBrightness = value;
            break;
		}
	}

    @Override
    public String GetType()
    {
        return "hsb";
    }

    @Override
    public boolean SolvesAttribute(String attribute)
    {
        return SolvesAttribute(attribute, solvableAttributes);
    }

    public static boolean GetIsApplicableToFixture(Fixture fixture)
    {
        return Solver.GetIsApplicableToFixture(fixture, solvableChannels);
    }

    @Override
    public float GetAttribute(String name)
    {
        switch(name)
        {
        case "Hue":
            return hue;
        case "Saturation":
            return saturation;
        case "Brightness":
            return brightness;
        case "OverrideEnabled":
            return overrideEnabled;
        case "OverrideHue":
            return overrideHue;
        case "OverrideSaturation":
            return overrideSaturation;
        case "OverrideBrightness":
            return overrideBrightness;
        default:
            return 0.0f;
        }
    }

    boolean IsOverrideEnabled()
    {
        return overrideEnabled > 0.0f;
    }
}
